package org.csu.agrisupply.api.controller.front;

import org.csu.agrisupply.api.common.CommonResponse;
import org.csu.agrisupply.api.entity.User;
import org.csu.agrisupply.api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/users/")
public class UserController {
    @Autowired
    private UserService userService;
    //得到所有用户
    @GetMapping("")
    @ResponseBody
    public CommonResponse<List<User>> getUserList(){
        return userService.getUserList();
    }
    //得到单个用户
    @GetMapping("{userId}")
    @ResponseBody
    public CommonResponse<User> getUser(@PathVariable("userId") String userId){

        return userService.getUser(userId);
    }
    //删除指定用户
    @DeleteMapping("{userId}")
    @ResponseBody
    public CommonResponse<User> deleteUser(@PathVariable("userId") String userId){
        return userService.deleteUser(userId);
    }
    //修改用户信息
    @PutMapping("{userId}")
    @ResponseBody
    public CommonResponse<User> updateUser(@PathVariable("userId") String userId ,
                                           @RequestBody User user){
        return userService.updateUser(userId,user);
    }
    //给用户分配身份
    @PutMapping("{userId}/role")
    @ResponseBody
    public CommonResponse<User> setRole(@PathVariable("userId") String userId ,
                                        @RequestParam(value = "role",required = true)int role){
        return userService.setRole(userId,role);
    }

}
